package controller;

import javafx.event.ActionEvent;
import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;

import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import model.dao.Database;
import view.OnLineView;
import view.PBoxView;
import view.RegisterView;
import view.ResetView;

import java.io.IOException;
import java.sql.ResultSet;

/**
 * 登录界面控制器
 * 检测输入内容是否合法，并向服务器传值
 */
public class LoginController {

    public PasswordField password;
    public TextField username;
    public Button logIn_btn;
    public CheckBox isRememberPwd;
    public Label register;
    public Label forgetPwd;
    public AnchorPane loginPane;


    /**
     * 登录方法
     * @param actionEvent
     * @throws Exception
     */
    public void logIn(ActionEvent actionEvent) throws Exception {
        String uname = username.getText();
        String pwd = password.getText();

        if (uname.isEmpty()) {
            String tip = "请输入用户名！";
            new PBoxView(tip).show();
        } else {
            if (pwd.isEmpty()) {
                new PBoxView("请输入密码！").show();
            } else {
                Database database = new Database();
                database.connect();
                String sql = "select user_id, password from user where account = ?";
                ResultSet resultSet = database.execResult(sql, uname);
                String ppwd = "";
                int id = -1;
                while (resultSet.next()) {
                    id = resultSet.getInt("user_id");
                    ppwd = resultSet.getString("password");
                }

                if (ppwd.equals(pwd)) {
                    String isRepeat = "select online_id from online where your_id = ?";
                    ResultSet res = database.execResult(isRepeat, String.valueOf(id));
                    if (res.next()) {
                        new PBoxView("您已在线，请不要重复登录！").show();
                        res.close();
                    } else {
                        res.close();
                        Stage stage = (Stage) loginPane.getScene().getWindow();
                        stage.close();
                        new OnLineView(id).show();
                        String online = "insert into online(your_id) values(?)";
                        database.exec(online, String.valueOf(id));
                    }
                } else {
                    String tip = "用户名或者密码错误，请重新输入";
                    new PBoxView(tip).show();
                }
            }
        }

    }

    /**
     * 注册方法
     * @param mouseEvent
     * @throws Exception
     */
    public void registerAccount(MouseEvent mouseEvent) throws Exception {
        System.out.println("注册中...");
        Stage stage = (Stage) loginPane.getScene().getWindow();
        stage.close();
        RegisterView rv = new RegisterView();
        rv.show();
    }

    /**
     * 忘记密码找回方法
     * @param mouseEvent
     */
    public void forgetPassword(MouseEvent mouseEvent) throws IOException {
        System.out.println("密码找回中...");
        Stage stage = (Stage) loginPane.getScene().getWindow();
        stage.close();
        ResetView rv = new ResetView();
        rv.show();
    }
}
